<?php
defined('BASEPATH') or exit('No direct script access allowed');

class FuturesData extends CI_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->model('Futures_history_model'); // 假设有一个名为Example_model的模型
		$this->load->model('Futures_variety_model');
		$this->load->model('Sync_date_model');

		$this->load->library('stringutil');

		header("Access-Control-Allow-Origin: *");
	        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
       		header("Access-Control-Allow-Headers: Content-Type, Authorization");
	}

	public function findBySymbol()
	{
		$symbol = $this->input->post('symbol');

		log_message("error", "findBySymbol, params: $symbol");
		$datas = $this->Futures_history_model->selectBySymbol($symbol);
		$datas = $datas ? $datas : array();
		log_message("error", "findBySymbol, result: " . json_encode($datas));

		// 使用CodeIgniter的output类来设置JSON响应
    	$this->output
			->set_content_type('application/json')
			->set_output(json_encode($datas));
	}

	public function findFuturesVariety()
	{
		$platform = $this->input->post('platform');

		log_message("error", "findFuturesVariety, params: $platform");
		$datas = $this->Futures_history_model->selectFuturesVarietyBySymbol($platform);
		$datas = $datas ? $datas : array();
		log_message("error", "findFuturesVariety, result: " . json_encode($datas));

		// 使用CodeIgniter的output类来设置JSON响应
    	$this->output
			->set_content_type('application/json')
			->set_output(json_encode($datas));
	}

	public function findFuturesVarietyDetail()
	{
		$platform = $this->input->post('platform');

		log_message("error", "findFuturesVariety, params: $platform");
		$datas = $this->Futures_history_model->selectFuturesVarietyBySymbol($platform);
		$datas = $datas ? $datas : array();

		$data_details = $this->Futures_history_model->selectFuturesVarietyDetailBySymbol($platform);
		$data_details = $data_details ? $data_details : array();

		foreach ($datas as $data) {

			$data->details = array();
			$variety_code = $data->code;

			foreach ($data_details as $detail) {
				if ($variety_code == $detail->variety_code) {
					array_push($data->details, $detail);
				}
			}
		}

		// 使用CodeIgniter的output类来设置JSON响应
    	$this->output
			->set_content_type('application/json')
			->set_output(json_encode($datas));
	}

	public function findBasisSpread()
	{
		$variety_code = $this->input->post('variety_code');
		$master_date = $this->input->post('master_date');
		$slave_date = $this->input->post('slave_date');

		$result = new stdClass();
		$result->is_success = false;

		// 1、获取品种代码
		$futures_variety = $this->Futures_variety_model->selectByCode($variety_code);
		if (! $futures_variety) {
			log_message("error", "findBasisSpread, Futures_variety_model->selectByCode fail: $variety_code");
			return $this->output
						->set_content_type('application/json')
						->set_output(json_encode($result));
		}
		$platform = $futures_variety->platform;

		// 2、组装近十年的日期
		$symbol_list = array();
		for ($i = 1; $i <= 10; $i++) {
			if ($platform == 'dce') {  // 大商所

			} elseif ($platform == 'czce') {  // 郑商所
				
			}
		}


		// 使用CodeIgniter的output类来设置JSON响应
    	$this->output
			->set_content_type('application/json')
			->set_output(json_encode($datas));
	}
}
